package com.moxi.mogublog.redis.web;

import com.moxi.mogublog.redis.service.IRedisService;
import com.moxi.mogublog.redis.vo.RedisInfoVO;
import com.moxi.mogublog.redis.web.prefix.UserKey;
import com.moxi.mogublog.utils.ResultUtil;
import com.moxi.mougblog.base.global.BaseSysConf;
import com.moxi.mougblog.base.result.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Redis测试接口
 *
 * @author Lishuai
 * @version 1.0.0
 * @date 2020-04-21
 */
@Api(value = "Redis控制层测试", tags = {"Redis操作接口"})
@RestController
public class RedisController {

    /**
     * log 变量不可变，读取速度快
     */
    private static final Logger log = LoggerFactory.getLogger(RedisController.class);

    @Autowired
    private IRedisService redisService;

    @ApiOperation(value = "Redis 获取详细信息", notes = "Redis 获取详细信息")
    @RequestMapping(value = "/redis/info", method = RequestMethod.GET)
    public ResultVO<List> getRedisInfo() {
        List<RedisInfoVO> redisInfoVOList = redisService.getRedisInfo();
        return ResultVO.successWithData(redisInfoVOList);
    }

    /**
     * 主要命令:检测redis服务器是否能ping通
     *
     * @return
     */
    @ApiOperation(value = "Redis ping", notes = "Redis ping命令")
    @RequestMapping(value = "/redis/ping", method = RequestMethod.GET)
    public String redisPing() {
        String ping = redisService.ping();
        return ResultUtil.result(BaseSysConf.SUCCESS, ping);
    }

    @ApiOperation(value = "Redis 删除指定前缀的key", notes = "Redis 删除指定前缀的key")
    @RequestMapping(value = "/redis/del/{keyPrefix}", method = RequestMethod.GET)
    public String delPrefix(@PathVariable("keyPrefix") String keyPrefix) {
//        redisService.delByKeyPrefix(keyPrefix);
        return ResultUtil.result(BaseSysConf.SUCCESS, null);
    }

    @ApiOperation(value = "Redis 获取指定前缀的key", notes = "Redis 获取指定前缀的key")
    @RequestMapping(value = "/redis/keys", method = RequestMethod.GET)
    public ResultVO keys(@RequestParam(value = "keyPrefix", required = true) String keyPrefix) {
        Set<String> setRet = redisService.keys(keyPrefix);
        return ResultVO.successWithData(setRet);
    }

    /**
     * 主要命令:获取 redis key 数量
     *
     * @return
     */
    @ApiOperation(value = "Redis 获取 key 的数量", notes = "Redis getKeysSize命令")
    @RequestMapping(value = "/redis/getKeysSize", method = RequestMethod.GET)
    public String redisGetKeysSize() {
        Map<String, Object> keysSize = redisService.getKeysSize();
        return ResultUtil.result(BaseSysConf.SUCCESS, keysSize);
    }

    /**
     * 主要命令:获取 redis 内存信息
     *
     * @return
     */
    @ApiOperation(value = "Redis 获取内存信息", notes = "Redis 获取内存信息")
    @RequestMapping(value = "/redis/getMemoryInfo", method = RequestMethod.GET)
    public String redisGetMemoryInfo() {
        Map<String, Object> memoryInfo = redisService.getMemoryInfo();
        return ResultUtil.result(BaseSysConf.SUCCESS, memoryInfo);
    }

    /**
     * 主要命令:获取指定的 key
     *
     * @param pattern 正则
     * @return
     */
    @ApiOperation(value = "Redis 正则匹配key", notes = "Redis 正则匹配key")
    @RequestMapping(value = "/redis/getGetKeys", method = RequestMethod.GET)
    public String redisGetKeys(String pattern) {
        Set<String> keys = redisService.getKeys(pattern);
        return ResultUtil.result(BaseSysConf.SUCCESS, keys);
    }

    /**
     * 主要命令：删除db下的所有数据
     *
     * @return
     */
    @ApiOperation(value = "Redis 清除当前库数据", notes = "Redis 清除当前库数据")
    @RequestMapping(value = "/redis/flushDB", method = RequestMethod.DELETE)
    public String flushDB() {
        String ret = redisService.flushDB();
        return ResultUtil.result(BaseSysConf.SUCCESS, ret);
    }

    /**
     * 主要命令:通杀全部库
     *
     * @return
     */
    @ApiOperation(value = "Redis 清除所有库的所有数据", notes = "Redis 清除所有库的所有数据")
    @RequestMapping(value = "/redis/flushAll", method = RequestMethod.DELETE)
    public String redisFlushAll() {
        String ret = redisService.flushAll();
        return ResultUtil.result(BaseSysConf.SUCCESS, ret);
    }

    /**
     * 主要命令:判断key是否存在
     *
     * @return
     */
    @ApiOperation(value = "Redis 判断key是否存在", notes = "Redis 判断key是否存在")
    @RequestMapping(value = "/redis/exists", method = RequestMethod.GET)
    public String redisExists() {
        String key = UserKey.getById.getPrefix() + "1";
        boolean exists = redisService.exists(key);
        return ResultUtil.result(BaseSysConf.SUCCESS, exists);
    }

    /**
     * 主要命令:查看key是什么类型
     *
     * @return
     */
    @ApiOperation(value = "Redis 查看key是什么类型", notes = "Redis 查看key是什么类型")
    @RequestMapping(value = "/redis/type", method = RequestMethod.GET)
    public String redisType() {
        String key = UserKey.getById.getPrefix() + "1";
        String type = redisService.type(key);
        return ResultUtil.result(BaseSysConf.SUCCESS, type);
    }

    /**
     * Redis数据类型1:字符串（String）-设置指定 key 的值
     *
     * @return
     */
    @ApiOperation(value = "Redis 字符串（String）-设置指定 key 的值", notes = "Redis 字符串（String）-设置指定 key 的值")
    @RequestMapping(value = "/redis/set", method = RequestMethod.POST)
    public ResultVO<String> setEx(
            @RequestParam(value = "key", required = true) String key,
            @RequestParam(value = "value", required = true) Object value) {
        String ret = redisService.set(key, value);
        return ResultVO.successWithData(ret);
    }

    /**
     * Redis数据类型1:字符串（String）-设置指定 key 的值
     *
     * @return
     */
    @ApiOperation(value = "Redis 字符串（String）-设置指定 key 的值", notes = "Redis 字符串（String）-设置指定 key 的值")
    @RequestMapping(value = "/redis/setEx", method = RequestMethod.POST)
    public ResultVO<String> setEx(
            @RequestParam(value = "key", required = true) String key,
            @RequestParam(value = "value", required = true) Object value,
            @RequestParam(value = "expireSeconds", required = true) int expireSeconds) {
        String ret = redisService.setEx(key, value, expireSeconds);
        return ResultVO.successWithData(ret);
    }

    /**
     * Redis数据类型1:字符串（String）-获取指定 key 的值
     *
     * @return
     */
    @ApiOperation(value = "Redis 字符串（String）-获取指定 key 的值", notes = "Redis 字符串（String）-获取指定 key 的值")
    @RequestMapping(value = "/redis/string/get", method = RequestMethod.GET)
    public ResultVO<String> get(@RequestParam(value = "key", required = true) String key) {
        String str = redisService.get(key);
        return ResultVO.successWithData(str);
    }


//    /**
//     * Redis数据类型1:字符串（String）-返回 key 所储存的字符串值的长度
//     *
//     * @return
//     */
//    @ApiOperation(
//            value = "Redis 字符串（String）-返回 key 所储存的字符串值的长度",
//            notes = "Redis 字符串（String）-返回 key 所储存的字符串值的长度")
//    @RequestMapping(value = "/strLen", method = RequestMethod.GET)
//    public String redisStrLen() {
//        String key = UserKey.getById.getPrefix() + 1;
//        Long len = redisService.strLen(key);
//        return ResultUtil.result(BaseSysConf.SUCCESS, len);
//    }

    @ApiOperation(value = "Redis 字符串（String）-根据指定的key删除", notes = "Redis 字符串（String）-根据指定的key删除")
    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    public ResultVO delete(@RequestParam(value = "key", required = true) String key) {
        boolean delCount = redisService.delete(key);
        return ResultVO.successWithData(delCount);
    }

    /**
     * Redis数据类型1:字符串（String）-根据指定的key删除
     *
     * @return
     */
    @ApiOperation(value = "Redis 字符串（String）-根据指定的key删除", notes = "Redis 字符串（String）-根据指定的key删除")
    @RequestMapping(value = "/dels", method = RequestMethod.DELETE)
    public ResultVO delete(@RequestParam(value = "keys", required = true) Set<String> keys) {
        Long delCount = redisService.delete(keys);
        return ResultVO.successWithData(delCount);
    }

//    /**
//     * Redis数据类型1:字符串（String）-增加值
//     *
//     * @return
//     */
//    @ApiOperation(value = "Redis 字符串（String）-增加值", notes = "Redis 字符串（String）-增加值")
//    @RequestMapping(value = "/incr", method = RequestMethod.PUT)
//    public String redisIncr() {
//        String key = UserKey.getById.getPrefix() + 100;
//        Long incr = redisService.incr(key);
//        return ResultUtil.result(BaseSysConf.SUCCESS, incr);
//    }
//
//    /**
//     * Redis数据类型1:字符串（String）-减少值
//     *
//     * @return
//     */
//    @ApiOperation(value = "Redis 字符串（String）-减少值", notes = "Redis 字符串（String）-减少值")
//    @RequestMapping(value = "/decr", method = RequestMethod.PUT)
//    public String redisDecr() {
//        String key = UserKey.getById.getPrefix() + 100;
//        Long decr = redisService.decr(key);
//        return ResultUtil.result(BaseSysConf.SUCCESS, decr);
//    }
//
//    /**
//     * Redis数据类型1:字符串（String）-增量值increment
//     *
//     * @return
//     */
//    @ApiOperation(value = "Redis 字符串（String）-增量值increment", notes = "Redis 字符串（String）-增量值increment")
//    @RequestMapping(value = "/incrBy", method = RequestMethod.PUT)
//    public String redisIncrBy() {
//        String key = UserKey.getById.getPrefix() + 100;
//        Long incrBy = redisService.incrBy(key, 1000L);
//        return ResultUtil.result(BaseSysConf.SUCCESS, incrBy);
//    }
//
//    /**
//     * Redis数据类型1:字符串（String）-减量值increment
//     *
//     * @return
//     */
//    @ApiOperation(value = "Redis 字符串（String）-减量值increment", notes = "Redis 字符串（String）-减量值increment")
//    @RequestMapping(value = "/decrBy", method = RequestMethod.PUT)
//    public String redisDecrBy() {
//        String key = UserKey.getById.getPrefix() + 100;
//        Long decrBy = redisService.decrBy(key, 20L);
//        return ResultUtil.result(BaseSysConf.SUCCESS, decrBy);
//    }
//
//
//    /**
//     * Redis数据类型2:哈希（Hash）:设置hash
//     *
//     * @return
//     */
//    @ApiOperation(value = "Redis 哈希（Hash）:设置hash", notes = "Redis 哈希（Hash）:设置hash")
//    @RequestMapping(value = "/hSet", method = RequestMethod.POST)
//    public String redisHSet() {
//        User user = new User();
//        user.setId(10002);
//        user.setName("1111");
//
//        String key = UserKey.getById.getPrefix() + 1;
//        Boolean result = redisService.hSet(key, "field", user, 100);
//        return ResultUtil.result(BaseSysConf.SUCCESS, result);
//    }
//
//    /**
//     * Redis数据类型2:哈希（Hash）:获取hash的属性值
//     *
//     * @return
//     */
//    @ApiOperation(value = "Redis 哈希（Hash）:获取hash的属性值", notes = "Redis 哈希（Hash）:获取hash的属性值")
//    @RequestMapping(value = "/hGet", method = RequestMethod.GET)
//    public String redisHGet() {
//        String key = UserKey.getById.getPrefix() + 1;
//        User result = redisService.hGet(key, "field", User.class);
//        return ResultUtil.result(BaseSysConf.SUCCESS, result);
//    }
//
//    /**
//     * Redis数据类型2:哈希（Hash）:批量设置hash
//     *
//     * @return
//     */
//    @RequestMapping(value = "/hmSet")
//    public String redisHmSet() {
//        User user1 = new User();
//        user1.setId(11001);
//        user1.setName("11001");
//
//        User user2 = new User();
//        user2.setId(11002);
//        user2.setName("11002");
//
//        Map<String, Object> values = new HashMap<>(2);
//        values.put("11001", user1);
//        values.put("11002", user2);
//
//        String key = UserKey.getById.getPrefix() + 11001;
//        String result = redisService.hmSet(key, values, 100);
//        return ResultUtil.result(BaseSysConf.SUCCESS, result);
//    }
//
//    /**
//     * Redis数据类型2:哈希（Hash）:获取hash的属性值
//     *
//     * @return
//     */
//    @RequestMapping(value = "/hmGet")
//    public String redisHmGet() {
//
//        Set<String> sets = new HashSet<>();
//        sets.add("11001");
//        sets.add("11002");
//
//        String key = UserKey.getById.getPrefix() + 11001;
//        List<String> stringList = redisService.hmGet(key, sets);
//
//        List<User> users = new ArrayList<>();
//        if (stringList.size() > 0) {
//            User user = null;
//            for (String userStr : stringList) {
//                user = DataTypeConvert.stringToBean(userStr, User.class);
//                users.add(user);
//            }
//        }
//        return ResultUtil.result(BaseSysConf.SUCCESS, users);
//    }
//
//    /**
//     * Redis数据类型2:哈希（Hash）:获取hash的所有属性
//     *
//     * @return
//     */
//    @RequestMapping(value = "/hGetAll")
//    public String redisHGetAll() {
//        String key = UserKey.getById.getPrefix() + 11001;
//        Map<String, String> map = redisService.hGetAll(key);
//        return ResultUtil.result(BaseSysConf.SUCCESS, map);
//    }
//
//    /**
//     * Redis数据类型2:哈希（Hash）:删除hash的某个属性
//     *
//     * @return
//     */
//    @RequestMapping(value = "/hDel1")
//    public String redisHDel1() {
//        String key = UserKey.getById.getPrefix() + 11001;
//        Long field = redisService.hDel(key, "field");
//        return ResultUtil.result(BaseSysConf.SUCCESS, field);
//    }
//
//    /**
//     * Redis数据类型2:哈希（Hash）:删除hash的某些属性
//     *
//     * @return
//     */
//    @RequestMapping(value = "/hDel2")
//    public String redisHDel2() {
//        Set<String> set = new HashSet<>();
//        set.add("11001");
//        set.add("11002");
//        String key = UserKey.getById.getPrefix() + 11001;
//        Long fields = redisService.hDel(key, set);
//        return ResultUtil.result(BaseSysConf.SUCCESS, fields);
//    }
//
//    /**
//     * Redis数据类型2:哈希（Hash）:针对hash中某个属性增加指定的值
//     *
//     * @return
//     */
//    @RequestMapping(value = "/hIncrBy")
//    public String redisHIncrBy() {
//        String key = UserKey.getById.getPrefix() + 11001;
//        Long hIncrBy = redisService.hIncrBy(key, "field", 10L);
//        return ResultUtil.result(BaseSysConf.SUCCESS, hIncrBy);
//    }
//
//    /**
//     * Redis数据类型2:哈希（Hash）:hash是存在某属性
//     *
//     * @return
//     */
//    @RequestMapping(value = "/hExists")
//    public String redisHExists() {
//        String key = UserKey.getById.getPrefix() + 11001;
//        Boolean hExists = redisService.hExists(key, "field");
//        return ResultUtil.result(BaseSysConf.SUCCESS, hExists);
//    }
//
//    /**
//     * Redis数据类型3:列表（List）：从list左侧插入一个元素
//     *
//     * @return
//     */
//    @RequestMapping(value = "/lPush1")
//    public String redisLPush1() {
//        String key = UserKey.getById.getPrefix() + "list";
//        Long lPush = redisService.lPush(key, "left1");
//        return ResultUtil.result(BaseSysConf.SUCCESS, lPush);
//    }
//
//    /**
//     * Redis数据类型3:列表（List）：从list左侧插入多个元素
//     *
//     * @return
//     */
//    @RequestMapping(value = "/lPush2")
//    public String redisLPush2() {
//        List<String> list = new ArrayList<>();
//        list.add("left2");
//        list.add("left3");
//        String key = UserKey.getById.getPrefix() + "list";
//        Long lPush = redisService.lPush(key, list);
//        return ResultUtil.result(BaseSysConf.SUCCESS, lPush);
//    }
//
//    /**
//     * Redis数据类型3:列表（List）：从list的左侧取出一个元素
//     *
//     * @return
//     */
//    @RequestMapping(value = "/lPop")
//    public String redisLPop() {
//        String key = UserKey.getById.getPrefix() + "list";
//        String lPop = redisService.lPop(key);
//        return ResultUtil.result(BaseSysConf.SUCCESS, lPop);
//    }
//
//    /**
//     * Redis数据类型3:列表（List）：向list的右侧插入一个元素
//     *
//     * @return
//     */
//    @RequestMapping(value = "/rPush1")
//    public String redisRPush1() {
//        String key = UserKey.getById.getPrefix() + "list";
//        Long rPush = redisService.rPush(key, "right1");
//        return ResultUtil.result(BaseSysConf.SUCCESS, rPush);
//    }
//
//    /**
//     * Redis数据类型3:列表（List）：向list的右侧插入多个元素
//     *
//     * @return
//     */
//    @RequestMapping(value = "/rPush2")
//    public String redisRPush2() {
//        List<String> list = new ArrayList<>();
//        list.add("right2");
//        list.add("right3");
//        String key = UserKey.getById.getPrefix() + "list";
//        Long rPush = redisService.rPush(key, list);
//        return ResultUtil.result(BaseSysConf.SUCCESS, rPush);
//    }
//
//    /**
//     * Redis数据类型3:列表（List）：从list的右侧取出一个元素
//     *
//     * @return
//     */
//    @RequestMapping(value = "/rPop")
//    public String redisRPop() {
//        String key = UserKey.getById.getPrefix() + "list";
//        String rPop = redisService.rPop(key);
//        return ResultUtil.result(BaseSysConf.SUCCESS, rPop);
//    }
//
//    /**
//     * Redis数据类型4:集合（Set）：给set中添加单个元素
//     *
//     * @return
//     */
//    @RequestMapping(value = "/sAdd1")
//    public String redisSAdd1() {
//        String key = UserKey.getById.getPrefix() + "list";
//        Long sAdd = redisService.sAdd(key, "set1");
//        return ResultUtil.result(BaseSysConf.SUCCESS, sAdd);
//    }
//
//    /**
//     * Redis数据类型4:集合（Set）：给set中添加多个元素
//     *
//     * @return
//     */
//    @RequestMapping(value = "/sAdd2")
//    public String redisSAdd2() {
//        Set<String> set = new HashSet<>();
//        set.add("set2");
//        set.add("set3");
//        set.add("set4");
//        String key = UserKey.getById.getPrefix() + "set";
//        Long sAdd = redisService.sAdd(key, set);
//        return ResultUtil.result(BaseSysConf.SUCCESS, sAdd);
//    }
//
//
//    /**
//     * Redis数据类型4:集合（Set）：获取set中的所有元素
//     *
//     * @return
//     */
//    @RequestMapping(value = "/sMembers")
//    public String redisSMembers() {
//        String key = UserKey.getById.getPrefix() + "set";
//        Set<String> sets = redisService.sMembers(key);
//        return ResultUtil.result(BaseSysConf.SUCCESS, sets);
//    }
//
//    /**
//     * Redis数据类型4:集合（Set）：set中是否包含某元素
//     *
//     * @return
//     */
//    @RequestMapping(value = "/sIsMember")
//    public String redisSIsMember() {
//        String key = UserKey.getById.getPrefix() + "set";
//        Boolean b = redisService.sIsMember(key, "set3");
//        return ResultUtil.result(BaseSysConf.SUCCESS, b);
//    }
//
//
//    /**
//     * Redis数据类型4:有序集合zSet（sorted set）:向zSet中的某个key添加一个属性几分数（可以根据分数排序）
//     *
//     * @return
//     */
//    @RequestMapping(value = "/zAdd")
//    public String redisZAdd() {
//        String key = UserKey.getById.getPrefix() + 11001;
//        Long zSet = redisService.zAdd(key, 10.0D, "zSet");
//        return ResultUtil.result(BaseSysConf.SUCCESS, zSet);
//    }
//
//    /**
//     * Redis数据类型4:有序集合zSet（sorted set）:给zSet中的某个key中的某个属性增加指定分数
//     *
//     * @return
//     */
//    @RequestMapping(value = "/zIncrBy")
//    public String redisZIncrBy() {
//        String key = UserKey.getById.getPrefix() + 11001;
//        Double zSet = redisService.zIncrBy(key, 10.0D, "zSet");
//        return ResultUtil.result(BaseSysConf.SUCCESS, zSet);
//    }

}

/**
 * 用户
 *
 * @author Lishuai
 * @version 1.0.0
 * @date 2020-04-21
 */
class User implements Serializable {

    private static final long serialVersionUID = 2767797872909181168L;

    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
